package org.csu.backofficemanagersys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.backofficemanagersys.domain.Admin;
import org.csu.backofficemanagersys.domain.Order;
import org.csu.backofficemanagersys.mapper.AdminMapper;
import org.csu.backofficemanagersys.mapper.OrderMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    //TODO 查，根据username找admin
    public Order selectOneByName(String username){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return orderMapper.selectOne(queryWrapper);
    }

    //TODO 获取所有订单
    public List<Order> orderList(){
        return orderMapper.selectList(null);
    }


    //TODO 删
    public Boolean delectAdminById(int id){
        return orderMapper.deleteById(id)!=0;
    }

    //TODO 改
    public Boolean updateOrder(Order order){
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",order.getId())
                .set("num",order.getNum())
                .set("total_cost",order.getTotal_cost())
                .set("address",order.getAddress())
                .set("productname",order.getProductname());
        return orderMapper.update(null,updateWrapper)!=0;
    }

    //TODO 发货
    public Boolean delivery(int id){
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("isdelivery",1);
        return orderMapper.update(null,updateWrapper)!=0;
    }

    //TODO 查找所有未发货订单
    public List<Order> Delivery(int delivery){
        return orderMapper.selectList(new QueryWrapper<Order>().eq("isdelivery",delivery));
    }

    public List<Order> findOrderLike(String username,int id){
        return orderMapper.selectList(new QueryWrapper<Order>().like("username",username).like("id",id));
    }
    public List<Order> findOrderLike(int id){
        return orderMapper.selectList(new QueryWrapper<Order>().like("id",id));
    }
    public List<Order> findOrderLike(String username){
        return orderMapper.selectList(new QueryWrapper<Order>().like("username",username));
    }


}
